package com.example.myfirstgame.model;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;

import com.example.myfirstgame.controller.CollisionObject;
import com.example.myfirstgame.controller.GameObject;
import com.example.myfirstgame.helper.RasterPoint;

public class ParkingLots {

	public static String TYPE_DEFINITION = "ParkingLots";
	public static int TYPE_01 = 0x32bc;
	public static int TYPE_02 = TYPE_01 + 1;
	public static int TYPE_03 = TYPE_02 + 1;

	private static int posX;
	private static int posY;
	private static ArrayList<ParkingLot> parkingLots = new ArrayList<ParkingLot>();
	private static Tree[] trees = null;


	public ParkingLots(int parkingLotType, Context appContext,
			RasterPoint pRasterPoint, int rows, int columns) {

		posX = (int) pRasterPoint.getCoordinate().getX();
		posY = (int) pRasterPoint.getCoordinate().getY();

		int startPosX = posX;
		int startPosY = posY;

		if (parkingLotType == TYPE_01) {

			// p p p
			// T
			// p p p
			for (int row = 1; row <= rows; row++) {
				if (parkingLots != null && parkingLots.size() > 0) {
					posY += parkingLots.get(0).getHeight();
					posX = startPosX;
				}
				for (int column = 1; column <= columns; column++) {
					ParkingLot pl = new ParkingLot(ParkingLot.TYPE_01,
							appContext, pRasterPoint);
					parkingLots.add(pl);
					posX += pl.getWidth();
				}
			}
			if (parkingLots != null && parkingLots.size() >= 4) {
				int posX = (int) (((float) rows / 2) * parkingLots.get(0)
						.getWidth());
				int posY = (int) ((columns / 2) * parkingLots.get(0)
						.getHeight());
				// posX += parkingLots.get(0).getPosition().getX();
				// posY += parkingLots.get(0).getPosition().getY();
				posX += startPosX;
				posY += startPosY;
				Tree t1 = new Tree(Tree.TYPE_TREE_01, appContext, posX, posY,
						pRasterPoint.getRasterSize());
				trees = new Tree[] { t1 };
			}

		} else if (parkingLotType == TYPE_02) {

		} else if (parkingLotType == TYPE_03) {

		}

	}

	public void draw(Canvas canvas) {
		if (parkingLots != null) {
			for (ParkingLot p : parkingLots) {
				p.draw(canvas);
			}
		}
		if (trees != null) {
			for (Tree t : trees) {
				t.draw(canvas);
			}
		}
	}

	public CollisionObject[] getAllIncludedCollidables() {
		ArrayList<CollisionObject> coList = new ArrayList<CollisionObject>();
		for (CollisionObject co : parkingLots) {
			coList.add(co);
		}
		for (CollisionObject co : trees) {
			coList.add(co);
		}
		CollisionObject[] coArr = new CollisionObject[coList.size()];
		return coList.toArray(coArr);
	}

	public ArrayList<GameObject> getAllIncludedGameObjects() {
		ArrayList<GameObject> goList = new ArrayList<GameObject>();
		for (GameObject go : parkingLots) {
			goList.add(go);
		}
		for (GameObject go : trees) {
			goList.add(go);
		}
		// GameObject[] goArr = new GameObject[goList.size()];
		return goList;
	}
}
